package com.wsz.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author wsz
 * time 2022-11-23 11:34
 */
public class MysqlUtils {
        //执行查询单行操作
        public <T> T TQuery(Class<T> cla,String sql,Object...args){
            Connection con = null;
            PreparedStatement ps = null;
            ResultSet res = null;
            try{
                con = JdbcUtils.getConnection();

                ps = con.prepareStatement(sql);

                for (int i = 0; i < args.length; i++) {
                    ps.setObject(i+1,args[i]);
                }

                res = ps.executeQuery();

                ResultSetMetaData rsmd = res.getMetaData();

                int col = rsmd.getColumnCount();
                while(res.next()){
                    T t = cla.getDeclaredConstructor().newInstance();

                    for (int i = 0; i < col; i++) {
                        Object colValue = res.getObject(i + 1);

                        String colName = rsmd.getColumnLabel(i + 1);
                        Field field = t.getClass().getDeclaredField(colName);
                        field.setAccessible(true);
                        if(field.getType() == Map.class){
                            JSONObject jsonObject = JSONObject.parseObject((String) colValue);
                            Map<String, Object> map = jsonObject;
                            field.set(t,map);
                        }else if(field.getType() == List.class){
                            List<String> list = JSON.parseArray((String) colValue, String.class);
                            field.set(t,list);
                        }else{
                            field.set(t,colValue);
                        }
                    }
                    return t;
                }

            }catch (Exception e){
                e.printStackTrace();
            }finally {
                JdbcUtils.close(con,ps,res);
            }
            return null;
        }
        //执行查询多行操作
        public <T> List<T> TAQuery(Class<T> cla, String sql, Object...args){
            Connection con = null;
            PreparedStatement ps = null;
            ResultSet res = null;
            try{
                con = JdbcUtils.getConnection();

                ps = con.prepareStatement(sql);

                for (int i = 0; i < args.length; i++) {
                    ps.setObject(i+1,args[i]);
                }

                res = ps.executeQuery();

                ResultSetMetaData rsmd = res.getMetaData();

                int col = rsmd.getColumnCount();

                ArrayList<T> list = new ArrayList<T>();

                while(res.next()){
                    T t = cla.getDeclaredConstructor().newInstance();

                    for (int i = 0; i < col; i++) {
                        Object colValue = res.getObject(i + 1);

                        String colName = rsmd.getColumnLabel(i + 1);
                        Field field = t.getClass().getDeclaredField(colName);
                        field.setAccessible(true);
                        if(field.getType() == Map.class){//判断该字段是否为map类型
                            JSONObject jsonObject = JSONObject.parseObject((String) colValue);//转换为jsonObject对象
                            Map<String, Object> map = jsonObject;//强转为map对象
                            field.set(t,map);
                        }else if(field.getType() == List.class){//判断该字段是否为mList类型
                            List<String> list1 = JSON.parseArray((String) colValue, String.class);//转换为jsonObject对象
                            field.set(t,list1);//强转为list对象
                        }else{
                            field.set(t,colValue);
                        }
                    }
                    list.add(t);
                }
                return list;

            }catch (Exception e){
                e.printStackTrace();
            }finally {
                JdbcUtils.close(con,ps,res);
            }
            return null;
        }

        //执行修改表操作
        public <T> T TUpdate(Class<T> cla,String sql,Object...args){
            Connection con = null;
            PreparedStatement ps = null;

            try{
                con = JdbcUtils.getConnection();
                ps = con.prepareStatement(sql);
                for (int i = 0; i < args.length; i++) {

                    if(HashMap.class.getTypeName().equals(args[i].getClass().getTypeName())
                            || ArrayList.class.getTypeName().equals(args[i].getClass().getTypeName())){
                        //判断该字段是否为map类型或List类型
                        String s = JSON.toJSONString(args[i]);//转换为Json字符串
                        ps.setObject(i + 1,s);
                        //
                    }else{
                        ps.setObject(i + 1,args[i]);
                    }
                }

                ps.execute();

            }catch (Exception e){
                e.printStackTrace();

            }finally {
                JdbcUtils.close(con,ps);
            }
            return null;
        }

    //插入一张图片
    public int InsertBlob(String sql){
        Connection con = null;
        PreparedStatement ps = null;
        int res = 0;
        try{
            con = JdbcUtils.getConnection();
            System.out.println(sql);
            ps = con.prepareStatement(sql);

            FileInputStream is = new FileInputStream(new File("1.png"));
            ps.setObject(1,is);
            res = ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JdbcUtils.close(con,ps);
        }
        return res;
    }


    public static Boolean CreateTable(String tableName,Class cla){
        System.out.println(tableName + "     =====    ");
            Connection con = null;
            String Sql = CreateTableUtils.getTableSql(tableName,cla);
            try{
                con = JdbcUtils.getConnection();
                Statement statement = con.createStatement();
                statement.execute(Sql);
                return true;
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                JdbcUtils.close(con);
            }
            return false;
    }

    public Boolean dropTable(String tableName){
            Connection con = null;
            String Sql = "DROP TABLE IF EXISTS "+tableName;
        try{
            con = JdbcUtils.getConnection();
            Statement statement = con.createStatement();
            statement.execute(Sql);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JdbcUtils.close(con);
        }
        return false;
    }


}
